﻿// Subsidiary functions and constants

var RATE_PERIOD = 89;	// the global constant which determines the period of dynamic's rate

function parseDateFromString(date) {
	var day = date.substring(0, 2);
	var month = date.substring(3, 5) - 1;
	var year = date.substring(6, 10);

	return new Date(year, month, day);
}

// Returns the date of Date type that was for n days ago
// parameter n is representing for how much days ago
function getDateForNDaysAgo(date, n) {
	if (typeof(date) == "string") {
		date = parseDateFromString(date);
	}
	return new Date(date.getTime() - n*24*60*60*1000);
}

// Returns obtained date in dd/mm/yyyy format of string type
function formatDateForCbrf(date) {
	if (typeof(date) == "string") {
		date = parseDateFromString(date);
	}

	var day = date.getDate();
	if (day < 10) day = "0" + day;
	var month = date.getMonth() + 1;
	if (month < 10) month = "0" + month;
	var year = date.getFullYear();

	return day + "/" + month + "/" + year;
}

// Function replacing in values of string type "," with "." for coversion to float
function replaceCommaWithDot(value) {
	if (typeof(value) == "number") {
		value = value.toString();
	}

	var indexOfComma = value.indexOf(",");
	if(indexOfComma != -1) {
		return value.substring(0, indexOfComma) + "." + value.substring(indexOfComma+1);
	}
	return value;
}

function replaceDotWithComma(value) {
	if (typeof(value) == "number") {
		value = value.toString();
	}

	var indexOfDot = value.indexOf(".");
	if(indexOfDot != -1) {
		return value.substring(0, indexOfDot) + "," + value.substring(indexOfDot+1);
	}
	return value;
}

function formatValueToRegStd(value) {
	if (typeof(value) == "number") {
		value = value.toString();
	}

	var curDecSymbol = prefs.getMsg("decimal_symbol");
	var curDecSymbolPos = value.indexOf(curDecSymbol);
	switch (curDecSymbol) {
		case ",":
			if (curDecSymbolPos == -1) {
				value = replaceDotWithComma(value);
			}
			break;
		case ".":
			if (curDecSymbolPos == -1) {
				value = replaceCommaWithDot(value);
			}
			break;
	}

	return value;
}

// Function makes the value with n digits after comma (adding zero if necessary)
function normalizeFloat(value, n) {
	if (typeof(value) == "number") {
		value = value.toString();
	}

	if (n <= 0) {
		return value;
	}

	var decSymbolPos = value.indexOf(".");
	if (decSymbolPos == -1) {
		value += ".";
		decSymbolPos = value.length - 1;
	}

	var mantissa = value.substr(decSymbolPos+1);
	if (mantissa.length < n) {
		for (var i = mantissa.length; i < n; i++) {
			value += "0";
		}
		return value;
	}
	
	return value.substring(0, decSymbolPos + 1 + n);
}

// Function returns true if parsing is unsuccesful and false otherwise
function responseError(response, tabId) {
	if (response == null || typeof(response) != "object" || response.firstChild == null) {
		_gel(tabId).innerHTML = "<div class='sys_message'>" + prefs.getMsg("parse_error") + "</div>";
		return true;
	}
	return false;
}

function showLoadingInfo(tabId) {
	_IG_Analytics("UA-1739928-1", "/finance-google-gadget/" + tabId);
	_gel(tabId).innerHTML = "<center><div class='sys_message'>" + prefs.getMsg("loading") + "</div></center>";
}